<?php

namespace app\controller\api\property;

use think\App;
use app\controller\api\Base;
use app\common\services\CacheService;
use think\exception\ValidateException;
use app\validate\property\basic\PropertyBasicValidate;
use app\common\repositories\property\basic\PropertyBasicRepository;

class PropertyBasic extends Base
{
    protected $repository;

    public function __construct(App $app, PropertyBasicRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 根据权限获取管理物业
     * @return mixed
     *
     * @author Amos
     * 2024-06-11 21:05
     */
    public function allPropertyList()
    {
        $where = $this->request->param([
            'keywords' => ''
        ]);
        [$page, $limit] = $this->getPage();
        $where['add_uid'] = $this->request->userId();
        $res = $this->repository->getApiAllPropertyList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     * 根据物业 ID 获取 房间入住情况
     * @return mixed
     *
     * @author Amos
     * 2024-06-11 21:05
     */
    public function getPropertySurvey(int $id)
    {
        if (!$this->repository->exists($id)) {
            return app('api_return')->error('数据不存在');
        }
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return app('api_return')->error('权限不足');
        }
        $res = CacheService::create($this->request->propertyId)
            ->remember('property_survey_' . $id, function () use ($id) {
                return $this->repository->getPropertySurvey($id);
            }, 300);
        return app('api_return')->success($res);
    }

    /**
     * 获取物业编码
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 21:05
     */
    public function getCode()
    {
        $res = $this->repository->getCoce(5);
        return app('api_return')->success($res);
    }


    /**
     *  添加物业
     * @return mixed
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-06-12 20:42
     */
    public function addProperty()
    {
        $param = $this->request->param([
            'name' => '',
            'code' => '',
            'firm_id' => '',
            'cate_id' => '',
            'address' => '',
            'picture' => '',
            'longitude' => '',
            'latitude' => '',
            'cover_area' => '',
            'building_area' => '',
            'remark' => '',
            'label_id' => '',
        ]);
        $param['code'] = strtoupper($param['code']);
        validate(PropertyBasicValidate::class)->scene('add')->check($param);
        if ($this->repository->fieldExists('name', $param['name'])) {
            return app('api_return')->error($param['name'] . ' 已存在');
        }
        $param['add_uid'] = $this->request->userId();
        if (!is_array($param['label_id'])) {
            $param['label_id'] = array_filter(explode(',', $param['label_id']));
        }
        $res = $this->repository->addInfo($param, $this->request->userId());
        return app('api_return')->success($res);
    }


    /**
     * 编辑物业
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 21:34
     */
    public function editProperty(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'name' => '',
            'firm_id' => '',
            'cate_id' => '',
            'address' => '',
            'picture' => '',
            'longitude' => '',
            'latitude' => '',
            'cover_area' => '',
            'building_area' => '',
            'remark' => '',
            'label_id' => '',
        ]);
        validate(PropertyBasicValidate::class)->scene('edit')->check($param);
        if ($this->repository->itemsFieldExists($this->request->propertyId, 'name', $param['name'], $id)) {
            return app('api_return')->error($param['name'] . ' 已存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('信息错误');
        }
        if (!is_array($param['label_id'])) {
            $param['label_id'] = array_filter(explode(',', $param['label_id']));
        }
        $res = $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success($res);
    }

    /**
     * 删除 物业
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 21:34
     */
    public function delProperty(int $id)
    {
        if (!$this->repository->exists($id)) {
            return app('api_return')->error('数据不存在');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }

        ##  检测出租中的房间
        $res = $this->repository->deleteCheckRoom($id);
        if ($res !== true) {
            return $this->error($res);
        }
        if ($info['add_uid'] != $this->request->userId()) {
            return app('api_return')->error('非法操作，非创建人禁止删除');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return app('api_return')->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

    public function detail(int $id)
    {
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return app('api_return')->error('权限不足');
        }
        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }

    /**
     * 获取物业列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-03 19:01
     */
    public function propertyList()
    {
        $where = $this->request->param([
            'cate_id' => '',
            'label_id' => '',
            'longitude' => '',
            'latitude' => '',
            'keywords' => ''
        ]);
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

}